package cn.uniqueww.javadesignpatterns.demos.controller;

import cn.uniqueww.javadesignpatterns.demos.design.factory.ParsingFactory;
import cn.uniqueww.javadesignpatterns.demos.design.fstGroup.ComputedAmount;
import cn.uniqueww.javadesignpatterns.demos.design.fstGroup.UserComputedAmountFactory;
import cn.uniqueww.javadesignpatterns.demos.design.fstGroup.UserType;
import cn.uniqueww.javadesignpatterns.demos.design.strategy.PayHandler;
import cn.uniqueww.javadesignpatterns.demos.design.template.AbstractCrawler;
import cn.uniqueww.javadesignpatterns.demos.design.template.JDCrawler;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 设计模式控制器
 * @since 2024/1/17 11:35 上午
 *
 */
@RequestMapping("/design")
@RestController
@RequiredArgsConstructor
public class DesignController {
    private  final ParsingFactory parsingFactory;

    private final UserComputedAmountFactory userComputedAmountFactory;

        /**
         * 测试工厂模式
         * @return
         */
        @GetMapping("/factory")
        public String factory(String type,String content){
            String parsing = parsingFactory.getParsing(type).parsing(content);
            return parsing;
        }

        /**
         * 测试策略模式
         * @param type
         * @param amount
         * @return
         */
        @GetMapping("/strategy")
        public String strategy(int type,int amount){
            String pay = PayHandler.getPay(type).pay(amount);
            return pay;
        }

        /**
         * 测试模板模式
         * @param url
         * @return
         */
        @GetMapping("/template")
        public String template(String url){
            AbstractCrawler  abstractCrawler = new JDCrawler();
            String crawler = abstractCrawler.crawlerTemplate(url);
            return crawler;
        }

        /**
         * 工厂模式 + 策略模式 + 模板模式的组合使用
         * @param type 用户类型
         * @param price 价格
         * @param count 数量
         * @return 折扣后的价格
         */
        @GetMapping("/factoryAndStrategyAndTemplate")
        public Double factoryAndStrategyAndTemplate(String type,int price,int count){
            UserType userType = UserType.valueOf(type);
            ComputedAmount computedAmount = userComputedAmountFactory.getComputedAmount(userType);
            return computedAmount.computedAmount(price, count);
        }

}
